﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Tyshka.Subroutines.Domain.Abstract;
using Tyshka.Subroutines.Domain.Entities;

namespace Tyshka.Subroutines.Domain.Concrete
{
    public class AlternativeRepository : IAlternativeRepository, IDisposable
    {
        private SubroutinesEntities context = new SubroutinesEntities();

        public Alternative Read(long id)
        {
            return context.Alternatives.FirstOrDefault(a => a.Id == id);
        }

        public IQueryable<Alternative> Query()
        {
            return context.Alternatives;
        }

        public IQueryable<Alternative> Query(Expression<Func<Alternative, bool>> filter)
        {
            return context.Alternatives.Where(filter);
        }

        public void Create(Alternative entity)
        {
            context.AddToAlternatives(entity);
            context.SaveChanges();
        }

        public void Delete(Alternative entity)
        {
            context.Alternatives.DeleteObject(entity);
            context.SaveChanges();
        }

        public void Update(Alternative entity)
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool cleanManaged)
        {
            if (cleanManaged)
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
    }
}
